/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOClass;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Phong
 */
public class vote extends Connect {

    private int voteID;
    private String voteName;
    private double voteSelected;
    private Connection Cnn = null;
    private ResultSet result = null;
    private Statement sqlQuerry;
    private PreparedStatement preSQL;

    public vote() {
        super();
    }

    public ResultSet voidSelect() {
        try {
            Cnn = this.getConnect();
            sqlQuerry = Cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            result = sqlQuerry.executeQuery("SELECT Vote.VoteID,SelectID,VoteName,VoteSelect,Selectted FROM Vote,VoteDetail WHERE Vote.VoteID = VoteDetail.VoteID");
        } catch (SQLException ex) {
            Logger.getLogger(vote.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }

    public boolean editVote(int idVote, String VoteName) {
        boolean flag = false;
        try {
            Cnn = this.getConnect();
            preSQL = Cnn.prepareStatement("UPDATE Vote SET VoteName = ? WHERE VoteID = ?");
            preSQL.setString(1, VoteName);
            preSQL.setInt(2, idVote);
            if (preSQL.executeUpdate() == 1) {
                flag = true;
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return flag;
    }

    public boolean checkVoite(int VoteID) {
        boolean flag = false;
        try {
            Cnn = this.getConnect();
            preSQL = Cnn.prepareStatement("SELECT * FROM VoteDetail WHERE VoteID = ?");
            preSQL.setInt(1, VoteID);
            result = preSQL.executeQuery();
            if (result.next()) {
                flag = true;
                return flag;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }
        return flag;
    }

    public boolean insertVote(String vName) {
        boolean flag = false;
        try {
            Cnn = this.getConnect();
            preSQL = Cnn.prepareStatement("INSERT INTO Vote(VoteName) VALUES(?)");
            preSQL.setString(1, vName);
            if (preSQL.executeUpdate() == 1) {
                flag = true;
            }
        } catch (SQLException ex) {
        }
        return flag;
    }

    public boolean insertVoteDetail(int vID, String voteName) {
        boolean flag = false;
        try {
            Cnn = this.getConnect();
            preSQL = Cnn.prepareStatement("INSERT INTO VoteDetail(VoteID,VoteName) VALUES(?,?)");
            preSQL.setInt(1, vID);
            preSQL.setString(2, voteName);
            if (preSQL.executeUpdate() == 1) {
                flag = true;
            }
        } catch (SQLException ex) {
        }
        return flag;
    }

    public boolean editVoteSelect(String SelectID ,String vSelect) {
        boolean flag = false;
        try {
            Cnn = this.getConnect();
            preSQL = Cnn.prepareStatement("UPDATE VoteDetail SET VoteSelect = ? WHERE SelectID = ?");
            preSQL.setString(1, vSelect);
            preSQL.setString(2, SelectID);

            if (preSQL.executeUpdate() == 1) {
                flag = true;
            }

        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return flag;
    }


    public String selectVote()   {
        String vName = "";
        try {
            Cnn = this.getConnect();
            sqlQuerry = Cnn.createStatement();
            result = sqlQuerry.executeQuery("SELECT * FROM Vote");
            while(result.next())    {
                vName = result.getNString("VoteName");
            }
        } catch (SQLException ex) {
            Logger.getLogger(vote.class.getName()).log(Level.SEVERE, null, ex);
        }
        return vName;
    }
    public void voite(int voteSelect)    {
        Cnn = this.getConnect();
        double selectted = 0;
        try {
            preSQL = Cnn.prepareStatement("SELECT Selectted FROM VoteDetail WHERE SelectID = ?");
            preSQL.setInt(1, voteSelect);
            result = preSQL.executeQuery();
            while (result.next())   {
                selectted = result.getInt("Selectted");
                System.out.println("SelectID : " +voteSelect +"\t Selectted : " +selectted);
                
            }


            selectted = selectted + 1;
            preSQL = Cnn.prepareStatement("UPDATE VoteDetail SET Selectted = ? WHERE SelectID = ?");
            preSQL.setDouble(1, selectted);
            preSQL.setInt(2, voteSelect);
            preSQL.executeUpdate();


            //----------------------------------------------
            preSQL = Cnn.prepareStatement("SELECT Selectted FROM VoteDetail WHERE SelectID = ?");
            preSQL.setInt(1, voteSelect);
            result = preSQL.executeQuery();
            while (result.next())   {
                selectted = result.getInt("Selectted");
                System.out.println("UPDATED - SelectID : " +voteSelect +"\t Selectted : " +selectted);

            }
        } catch (SQLException ex)   {
        }
    }



    public static void main(String[] args) throws SQLException {
        vote v = new vote();
        v.voite(2);
    }
}

